package com.gl.wxstore.sys.controller;

import com.gl.wxstore.common.BaseController;
import com.gl.wxstore.common.model.StoreCart;
import com.gl.wxstore.common.model.StoreOrderProduct;
import com.gl.wxstore.common.model.StoreProduct;
import com.gl.wxstore.common.model.StoreReceiveAddress;
import com.gl.wxstore.common.utils.PageConstants;
import com.gl.wxstore.common.utils.PageModel;
import com.gl.wxstore.sys.service.ProduceService;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 *
 * @author Liberty
 * @since Jan 9, 2017
 */
public class ProduceController extends BaseController {
	private ProduceService produceService = Duang.duang(ProduceService.class);

	public void listProduct() {
		Integer pageNo = getParaToInt();
		if (pageNo == null) {
			pageNo = PageConstants.PAGE_NUMBER;
		}
		Page<Record> producePage = produceService.list(pageNo);
		PageModel<Record> producePageModel = new PageModel<>(
				producePage.getList(), producePage.getPageNumber(),
				producePage.getPageSize(), producePage.getTotalPage(),
				producePage.getTotalRow());
		setAttr("producePageModel", producePageModel);
		render("/views/store/index.jsp");
	}
	
	public void listStoreCart() {
		String userId = getPara("id");
		Integer pageNo = getParaToInt();
		if (pageNo == null) {
			pageNo = PageConstants.PAGE_NUMBER;
		}
		Page<Record> storeCartPage = produceService.listStoreCart(pageNo,userId);
		PageModel<Record> storeCartPageModel = new PageModel<>(
				storeCartPage.getList(), storeCartPage.getPageNumber(),
				storeCartPage.getPageSize(), storeCartPage.getTotalPage(),
				storeCartPage.getTotalRow());
		setAttr("storeCartPageModel", storeCartPageModel);
		Integer ttp = produceService.ttp(storeCartPageModel);
		setAttr("ttp",ttp);
		render("/views/store/shopcar.jsp");
	}

	public void listNewProduct() {
		Integer pageNo = getParaToInt("pageNo");
		Page<Record> producePage = produceService.list(pageNo);
		PageModel<Record> newProducePageModel = new PageModel<>(
				producePage.getList(), producePage.getPageNumber(),
				producePage.getPageSize(), producePage.getTotalPage(),
				producePage.getTotalRow());
		renderJson(newProducePageModel);
	}

	public void getProduct() {
		String id = getPara("id");
		StoreProduct product = new StoreProduct();
		setAttr("getProduct", product.dao.findById(id));
		render("/views/store/info.jsp");
	}
	
	public void addstoreCart() {
		String userId = getPara("userId");
		String proId = getPara("proId");
		Integer mulit = Integer.parseInt(getPara("mulit"));
		StoreCart sc = getModel(StoreCart.class,
				"sc");
		sc.setStoreProductId(proId);
		sc.setSysUserId(userId);
		sc.setCount(mulit);
		StoreCart.dao.add(sc);
		renderJson("success");
	}
	
	public void upcount(){
		String id = getPara("id");
		StoreCart sc = StoreCart.dao.findById(id);
		sc.set("count",sc.getCount()+1).update();
		renderJson("success");
	}
	
	public void downcount(){
		String id = getPara("id");
		StoreCart sc = StoreCart.dao.findById(id);
		sc.set("count",sc.getCount()-1).update();
		renderJson("success");
	}

}
